/**
 * HVAC 系统 API 接口
 */
import request from './request';
import type {
  RoomState,
  DetailRecord,
  Bill,
  CheckInRecord,
  StatisticsReport,
  ServiceRequest,
} from '../types';
import { ACMode, FanSpeed } from '../types';

/**
 * 前台管理接口
 */
export const frontDeskApi = {
  // 办理入住
  checkIn(roomId: string, guestName: string, guestPhone: string, idCard: string, stayDays: number, roomType: string, mode?: ACMode) {
    return request.post('/frontdesk/checkin', { 
      roomId, 
      guestName, 
      guestPhone, 
      idCard, 
      stayDays, 
      roomType, 
      mode: mode || ACMode.COOLING 
    });
  },

  // 办理退房
  checkOut(roomId: string) {
    return request.post<Bill>(`/frontdesk/checkout/${roomId}`);
  },

  // 获取账单
  getBill(roomId: string) {
    return request.get<Bill>(`/frontdesk/bill/${roomId}`);
  },

  // 获取所有账单
  getAllBills() {
    return request.get<Bill[]>('/frontdesk/bills');
  },

  // 获取已入住房间列表
  getOccupiedRooms() {
    return request.get<string[]>('/frontdesk/occupied-rooms');
  },

  // 获取入住记录
  getCheckInRecords() {
    return request.get<CheckInRecord[]>('/frontdesk/checkin-records');
  },

  // 获取详单记录
  getDetailRecords(roomId: string) {
    return request.get<DetailRecord[]>(`/frontdesk/detail/${roomId}`);
  },
};

/**
 * 房间客户端接口
 */
export const roomApi = {
  // 获取房间状态
  getRoomState(roomId: string) {
    return request.get<RoomState>(`/room/${roomId}/state`);
  },

  // 开机 (后端不接受额外参数，只接受 roomId)
  turnOn(roomId: string) {
    return request.post(`/room/${roomId}/turnon`);
  },

  // 关机
  turnOff(roomId: string) {
    return request.post(`/room/${roomId}/turnoff`);
  },

  // 发送请求（调温、调风）
  sendRequest(roomId: string, targetTemp: number, fanSpeed: FanSpeed) {
    return request.post(`/room/${roomId}/request`, { targetTemp, fanSpeed });
  },
};

/**
 * 管理员监控接口
 */
export const adminApi = {
  // 获取所有房间状态（从 room 接口获取）
  getAllRoomStates() {
    return request.get<RoomState[]>('/room/states');
  },

  // 获取服务队列
  getServiceQueue() {
    return request.get<any[]>('/admin/service-queue');
  },

  // 获取等待队列
  getWaitingQueue() {
    return request.get<any[]>('/admin/waiting-queue');
  },

  // 一键关机
  turnOffAll() {
    return request.post('/admin/turnoff-all');
  },

  // 一键开机
  turnOnAll() {
    return request.post('/admin/turnon-all');
  },

  // 清空等待队列
  clearWaitingQueue() {
    return request.post('/admin/clear-waiting-queue');
  },
};

/**
 * 经理统计接口
 */
export const managerApi = {
  // 获取统计报表（后端使用 POST 方法）
  getStatistics(startTime: string, endTime: string, roomId?: string) {
    return request.post<StatisticsReport>('/manager/statistics', {
      startTime,
      endTime,
      roomId,
    });
  },

  // 获取所有账单
  getAllBills() {
    return request.get<Bill[]>('/manager/bills');
  },
};

/**
 * 导出所有 API
 */
export default {
  frontDesk: frontDeskApi,
  room: roomApi,
  admin: adminApi,
  manager: managerApi,
};

